package com.czxy.service;

import com.czxy.dao.UserMapper;
import com.czxy.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;


    @Override
    public List<User> findAll() {
        return userMapper.selectAll() ;
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findById(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void deleteById(Integer uid) {
        userMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public User login(User user) {

        //拼装条件

        Example example = new Example(User.class);

        Example.Criteria ct = example.createCriteria();


        ct.andEqualTo("loginName",user.getLoginName());
        ct.andEqualTo("loginPwd",user.getLoginPwd());


        List<User> list = userMapper.selectByExample(example);

        if (list.size() > 0){
            return  list.get(0);
        }else{
            return null;


        }





    }
}
